package jmu.cyxwcy.librarymanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jmu.cyxwcy.librarymanagement.pojo.BookDescription;
import jmu.cyxwcy.librarymanagement.pojo.Result;
import jmu.cyxwcy.librarymanagement.service.BookDescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bookdescription")
@Validated
public class BookDescriptionController {
    @Autowired
    private BookDescriptionService bookDescriptionService;

    @PostMapping("/add")
    public Result addBookDescription(@RequestBody BookDescription bookDescription){
        bookDescriptionService.getBaseMapper().insert(bookDescription);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateBookDescription(@RequestBody BookDescription bookDescription){
        bookDescriptionService.updateById(bookDescription);
        return Result.success();
    }

    @DeleteMapping("/delete/{isbn}")
    public Result deleteBookDescription(@PathVariable String isbn){
        bookDescriptionService.removeById(isbn);
        return Result.success();
    }
    @GetMapping("/findbyisbn")
    public Result findBookDescriptionByisbn(@RequestParam String isbn){
        BookDescription bookDescription = bookDescriptionService.getById(isbn);
        return Result.success(bookDescription);
    }

    @GetMapping("/findbyisbnpartial")
    public Result findBookDescriptionByPartialIsbn(@RequestParam String isbnPartial) {
        List<BookDescription> bookDescriptions = bookDescriptionService.findByIsbnPartial(isbnPartial);
        return Result.success(bookDescriptions);
    }

    @GetMapping("/page")
    public Result<IPage<BookDescription>> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String isbn,
                                        @RequestParam(defaultValue = "") String bookName,
                                        @RequestParam(defaultValue = "") String price,
                                        @RequestParam(defaultValue = "") String description,
                                        @RequestParam(defaultValue = "") String language) {
        IPage<BookDescription> page = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<BookDescription> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .like(!price.isEmpty(), BookDescription::getPrice,price)
                .like(!description.isEmpty(), BookDescription::getDescription,description)
                .like(!isbn.isEmpty(), BookDescription::getIsbn,isbn)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(!language.isEmpty(), BookDescription::getLanguage, language);
        queryWrapper.orderByAsc("isbn");
        IPage<BookDescription> pg= bookDescriptionService.join(page, queryWrapper);
        return Result.success(pg);
    }


}
